const Koa = require('koa')
const app = new Koa()
const path = require('path')

// 解决跨域01 npm install koa2-cors --save 
const cors = require('koa2-cors')
app.use(cors())
// 解决跨域02
// app.use(async (ctx, next) => { 
//   ctx.set("Access-Control-Allow-Origin", "*") 
//   ctx.set("Access-Control-Allow-Methods", "OPTIONS, GET, PUT, POST, DELETE") 
//   await next(); 
// })

// koa-bodyparser
const koaBodyparser = require('koa-bodyparser')
app.use(koaBodyparser())

// 导入路由
const Router = require('koa-router')
const router = new Router()

// 装载子路由
const userRouter = require('./routes/router/user')
router.use('/api', userRouter) //与express不同，这里使用 xxx.routes()


// 全局加载路由中间件
app.use(router.routes())
app.use(router.allowedMethods())

app.listen(3000, ()=> {
  console.log('[demo]在3000端口运行中...')
})